package com.edu.hre.struts.action.pim;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.edu.hre.common.utils.BeanFinder;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.common.utils.RequestUtils;
import com.edu.hre.common.utils.StringConstants;
import com.edu.hre.db.pojo.HreEmpEmergencyContacts;
import com.edu.hre.db.pojo.HreUser;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.pim.EmpEmergencyContactUpdateForm;

public class EmpEmergencyContactUpdateAction extends DispatchAction{
	
	private static final Logger logger = Logger.getLogger(EmpEmergencyContactUpdateAction.class);
	private static final BaseService baseService = BeanFinder.getBean(BaseService.BEAN_NAME, BaseService.class);
	private final String ACTION_NAME = "empEmergencyContactUpdate";
	
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

		CommonUtils.println(CommonUtils.getSourceCodeLocation());

		EmpEmergencyContactUpdateForm beanForm = (EmpEmergencyContactUpdateForm) form;
		beanForm.setIsAddNew(true);

		if(request.getParameter(StringConstants.BTN_ADD) == null){
			
			int empNum = CommonUtils.getIntParameter("emp_num", request);
			beanForm.setEmpNumber(empNum);

			beanForm.setMessage(StringConstants.BLANK);
			return mapping.findForward(ACTION_NAME);
		}
		
		EmployeeListAction.setAttributesForRequest(beanForm.getEmpNumber(), request);
		
		String message = validateForm(beanForm);
		if(!StringUtils.isBlank(message)){
			beanForm.setMessage(message);
			return mapping.findForward(ACTION_NAME);
		}

		HreEmpEmergencyContacts tmp = new HreEmpEmergencyContacts();
		tmp.setEmpNumber(beanForm.getEmpNumber());
		tmp.setEecName(CommonUtils.normalizeString(beanForm.getName()));
		tmp.setEecRelationship(CommonUtils.normalizeString(beanForm.getRelationship()));
		tmp.setEecHomeNo(CommonUtils.normalizeString(beanForm.getHomePhone()));
		tmp.setEecMobileNo(CommonUtils.normalizeString(beanForm.getMobilePhone()));
		tmp.setEecOfficeNo(CommonUtils.normalizeString(beanForm.getWorkPhone()));
		tmp.setDateCreated(new Date());
		tmp.setDateModified(new Date());

		boolean status = baseService.saveOrUpdate(tmp);
		if (!status) {
			beanForm.setMessage(StringConstants.INSERT_FAILURE_MESSAGE);
			return mapping.findForward(ACTION_NAME);
		}

		response.getWriter().write(StringConstants.POP_UP_CLOSE_SCRIPT);
		return null;
	}

	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		CommonUtils.println(CommonUtils.getSourceCodeLocation());
		EmpEmergencyContactUpdateForm beanForm = (EmpEmergencyContactUpdateForm) form;
		beanForm.setIsAddNew(false);
		
		if(request.getParameter(StringConstants.BTN_EDIT) == null){

			int id = CommonUtils.getIntParameter("id", request);
			HreEmpEmergencyContacts tmp = baseService.getFirstPojo("id", Compare.EQUAL, (long) id, null, null, 0, HreEmpEmergencyContacts.class);

			if (tmp == null) {
				beanForm.setMessage(StringConstants.BLANK);
				return mapping.findForward(ACTION_NAME);
			}

			beanForm.setId(id);
			beanForm.setEmpNumber(tmp.getEmpNumber());
			beanForm.setName(CommonUtils.normalizeString(tmp.getEecName()));
			beanForm.setRelationship(CommonUtils.normalizeString(tmp.getEecRelationship()));
			beanForm.setHomePhone(CommonUtils.normalizeString(tmp.getEecHomeNo()));
			beanForm.setMobilePhone(CommonUtils.normalizeString(tmp.getEecMobileNo()));
			beanForm.setWorkPhone(CommonUtils.normalizeString(tmp.getEecOfficeNo()));

			beanForm.setMessage(StringConstants.BLANK);
			return mapping.findForward(ACTION_NAME);
		}

		EmployeeListAction.setAttributesForRequest(beanForm.getEmpNumber(), request);

		String message = validateForm(beanForm);
		if(!StringUtils.isBlank(message)){
			beanForm.setMessage(message);
			return mapping.findForward(ACTION_NAME);
		}

		HreEmpEmergencyContacts tmp = baseService.getFirstPojo("id", Compare.EQUAL, (long) beanForm.getId(), null, null, 0, HreEmpEmergencyContacts.class);
		if (tmp == null) {
			beanForm.setMessage(StringConstants.BLANK);
			return mapping.findForward(ACTION_NAME);
		}

		tmp.setEecName(CommonUtils.normalizeString(beanForm.getName()));
		tmp.setEecRelationship(CommonUtils.normalizeString(beanForm.getRelationship()));
		tmp.setEecHomeNo(CommonUtils.normalizeString(beanForm.getHomePhone()));
		tmp.setEecMobileNo(CommonUtils.normalizeString(beanForm.getMobilePhone()));
		tmp.setEecOfficeNo(CommonUtils.normalizeString(beanForm.getWorkPhone()));
		tmp.setDateModified(new Date());

		boolean status = baseService.saveOrUpdate(tmp);
		if (!status) {
			beanForm.setMessage(StringConstants.UPDATE_FAILURE_MESSAGE);
			return mapping.findForward(ACTION_NAME);
		}

		response.getWriter().write(
				 "<script language=\"javascript\">\n" +
						" parent.dw.close();" + 
						 " var param = parent.document.createElement('input'); " +
						 " param.type = 'hidden'; " +
						 " param.name = 'id'; " +
						 " param.value = " + tmp.getEmpNumber() +  ";" +
						 " parent.document.forms[0].appendChild(param);" +
						" parent.document.forms[0].submit(); \n" +
				 "</script>");
		return null;
	}
	
	private String validateForm (EmpEmergencyContactUpdateForm beanForm) {

		if(StringUtils.isBlank(CommonUtils.normalizeString(beanForm.getName()))) {
			return "Contact name cannot be blank!";
		}

		if(StringUtils.isBlank(CommonUtils.normalizeString(beanForm.getRelationship()))) {
			return "Relationship cannot be blank!";
		}
		
		if(!StringUtils.isBlank(CommonUtils.normalizeString(beanForm.getHomePhone())) && !CommonUtils.verifyPhoneNumber(CommonUtils.normalizeString(beanForm.getHomePhone()))){
			return "Home phone is not valid!";
		}
		
		if(!StringUtils.isBlank(CommonUtils.normalizeString(beanForm.getMobilePhone())) && !CommonUtils.verifyPhoneNumber(CommonUtils.normalizeString(beanForm.getMobilePhone()))){
			return "Mobile phone is not valid!";
		}
		
		if(!StringUtils.isBlank(CommonUtils.normalizeString(beanForm.getWorkPhone())) && !CommonUtils.verifyPhoneNumber(CommonUtils.normalizeString(beanForm.getWorkPhone()))){
			return "Work phone is not valid!";
		}

		return StringConstants.BLANK;
	}
}